package com.shengruan.admin.action;

import com.shengruan.admin.jdbc.SystemEmailJdbc;
import com.shengruan.admin.vo.SystemEmail;
import com.shengruan.common.BaseAction;
import com.shengruan.common.Mail;

public class SystemEmailAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void init() throws Exception 
	{
		SystemEmailJdbc systemEmailJdbc = new SystemEmailJdbc();
		SystemEmail systemEmail = systemEmailJdbc.getSystemEmail();
		request.setAttribute("systemEmail", systemEmail);
		request.getRequestDispatcher("/admin/system/systemEmail.jsp").forward(request, response);
	}


	/***
	 * 发送邮件
	 */
	public void send() throws Exception
	{
		SystemEmailJdbc systemEmailJdbc = new SystemEmailJdbc();
		SystemEmail systemEmail = systemEmailJdbc.getSystemEmail();
		
		String email = request.getParameter("email");
		String title = request.getParameter("title");
		String content = request.getParameter("content");

		Mail sendmail = new Mail();
		try
		{
			sendmail.setHost(systemEmail.getServer());
			sendmail.setUserName(systemEmail.getUserName());
			sendmail.setPassWord(systemEmail.getPassword());
			sendmail.setFrom(systemEmail.getUserName());
			sendmail.setTo(email);
			sendmail.setSubject(title);
			sendmail.setContent(content); 
			boolean result = sendmail.sendMail();
			if(result)
			{
				this.print("success");
				
			}
			else
			{
				this.print("error");
				
			}
		}
		catch(Exception ex)
		{
			this.print("error");
		}
	
		
	}

	public static void main(String [] args)
	{
		//		Mail sendmail = new Mail();
		//		sendmail.setHost("bjbranch.cbmi.com.cn");
		//		sendmail.setUserName("elib@cbmi.com.cn");
		//		sendmail.setPassWord("sssss");
		//		sendmail.setFrom("elib@cbmi.com.cn");
		//		sendmail.setTo("epopos@vip.qq.com");
		//		sendmail.setSubject("111");
		//		sendmail.setContent("ss"); 
		//		sendmail.sendMail();

	}
	/***
	 * 更新
	 */
	public void update() throws Exception 
	{
		SystemEmailJdbc systemEmailJdbc = new SystemEmailJdbc();
		SystemEmail systemEmail = systemEmailJdbc.getSystemEmail();
		String server = request.getParameter("server");
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		systemEmail.setServer(server);
		systemEmail.setUserName(userName);
		if(password!=null&&!"UNCHANGE".equals(password))
			systemEmail.setPassword(password);
		systemEmailJdbc.update(systemEmail);
		this.redirect("/admin/system/systemEmail.ac");
	}
}
